<!--
 醫學美容糾察隊 - PIXNET's layout engine
 Main JavaScript section
 by HappyDenn | 20100118
-->

<!-- save the original pixnet's jQuery -->
<script type="text/javascript">var jQuery_pix = jQuery.noConflict(true);</script>
<!-- load some libraries -->
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.0/jquery.min.js"></script>
<script type="text/javascript" src="http://dl.dropbox.com/u/255959/wowbuty/jquery.jtemplates.js"></script>

<!-- main script -->
<script type="text/javascript">
// ------ REMAP jQUERY FUNCTION ------
var jQuery_wowbuty = jQuery.noConflict(true);
jQuery = jQuery_pix.noConflict(true);

(function($) {

// ------ CUSTOM FUNCTIONS ------
// trim leading and trailing whitespace function
function strTrim(s) {
    return s.replace(/^\s+/, '').replace(/\s+$/, '');
}

// parse <div class="article"> element
function parseArticleElement(element) {
    // create article object
    var article = new Object();
    // parse publish date
    var elPublish = $(element).find('li.publish');
    article.publishYear = strTrim(elPublish.children('.year').text());
    article.publishMonth = strTrim(elPublish.children('.month').text());
    article.publishDate = strTrim(elPublish.children('.date').text());
    article.publishDay = strTrim(elPublish.children('.day').text());
    article.publishTime = strTrim(elPublish.children('.time').text());
    // parse article title and permalink
    var elTitle = $(element).find('li.title h2');
    article.title = strTrim(elTitle.text());
    article.permalink = strTrim(elTitle.children('a').attr('href'));
    // parse article content and thumbnail
    var elContent = $(element).find('div.article-content');
    var thumb = elContent.find('img:first');
    if (thumb.length) {
        article.thumbnail = strTrim(thumb.attr('src'));
        thumb.addClass('article-thumbnail');
        //thumb.hide();
    }
    article.content = strTrim(elContent.html());
    // return the parsed article object
    return article;
}

// ------ WOWBUTY INITIALIZER ------ 
// wowbuty object
var wowbuty = new Object();

// ------ LOADING NOTIFICATION ------
// disable for IE6
if (!($.browser.msie && $.browser.version < 7)) {
    wowbuty.loadingIndicator = $('<div/>').attr('id', 'loading-indicator')
        .html('頁面讀取中...')
        .insertBefore('#body-div')
        .show();
}

// ------ ATTACH TEMPLATE ------
// inject the new container before pixnet's old container
wowbuty.container = $('<div/>').attr('id', 'wowbuty-container').insertBefore('#body-div');
// attach page template
wowbuty.container.setTemplateElement('template-area', undefined, {filter_data: false});

// ------ PAGE TYPE DETERMINATION ------
// determine page type
wowbuty.pageType = 'home';
wowbuty.baseUrl = 'http://wowbutydev.pixnet.net/blog';
var currentPath = window.location.href.substr(wowbuty.baseUrl.length);
if (currentPath == '') {
    wowbuty.pageType = 'home';
} else if (currentPath.search(/^\/category\/.+$/) != -1) {
    wowbuty.pageType = 'category';
} else if (currentPath.search(/^\/post\/.+$/) != -1) {
    wowbuty.pageType = 'article';
}

// ------ DATA PROCESSING ------
// --- data object ---
var data = new Object();
// --- COMMON PAGE ELEMENTS ---
// page type
data.pageType = wowbuty.pageType;
// login bar
var loginBar = $('#login-bar').clone();
loginBar.find('script').remove();
data.loginBar = strTrim(loginBar.html());
// categories
data.categories = new Array();
$.each($('#category .inner-box'), function(i) {
    var category = new Object();
    category.heading = strTrim($(this).children('h6').text().replace(/^(.+)(\(.+\))$/, '$1'));
    category.children = new Array();
    $.each($(this).children('ul').children('li'), function(i) {
        category.children[i] = new Object();
        category.children[i].href = strTrim($(this).children('a').attr('href'));
        category.children[i].title = strTrim($(this).text());
    });
    data.categories[i] = category;
});
// article list
if (wowbuty.pageType == 'category' || wowbuty.pageType == 'home') {
    data.articles = new Array();
    $.each($('#article-box .article'), function(i) {
        data.articles[i] = parseArticleElement(this);
    });
    data.articleCount = data.articles.length;
}
// current category
if (wowbuty.pageType == 'category') {
    data.currentCategory = $('h3.article-area-title').text().replace(/目前分類：/, '');
}

// ------ PROCESS TEMPLATE AND OUTPUT ------
//console.log(data);
wowbuty.container.processTemplate(data);

// ------ IE AND OTHER BROWSER ISSUES FIX ------
// internet explorer
if ($.browser.msie) {
    // all versions
    
    if (parseInt($.browser.version) <= 7) {
        // 7.0 or below
        
        switch (parseInt($.browser.version)) {
            // 6.0
            case 6:
                $('#facebook-link').addClass('ie6');
                break;
            
            // 7.0
            case 7:
                $('#top-panel').addClass('ie7fix');
                break;
            
            // below 6.0
            default:
                
                break;
        }
    } else {
        // 8.0
        
    }
}

// ------ EFFECTS AND BROWSER EVENTS ------
// box container fade in/fade out
// opacity idea from:
//   http://www.stevenyork.com/tutorial/pure_css_opacity_and_how_to_have_opaque_children
var boxContentOpacity = 0.83;
var boxContentFadeSpeed = 200;
var articleBgOpacity = 0.45;
$.each($('.box-container'), function() {
    var contentHtml = $(this).html();
    var transDiv = $('<div/>')
        .addClass('background').width($(this).innerWidth()).height($(this).innerHeight())
        .fadeTo(0, boxContentOpacity);
    var contentDiv = $('<div/>').addClass('content').html(contentHtml);
    if ($(this).hasClass('article-list-item')) {
        var thumb = $(this).find('.article-thumbnail');
        if (thumb.length) {
            var thumbDiv = $('<div/>')
                .css({
                    position: 'absolute',
                    bottom: 5,
                    right: 5,
                    width: 110,
                    height: 110,
                    overflow: 'hidden',
                    textAlign: 'center'
                })
                .fadeTo(0, articleBgOpacity)
                .appendTo(transDiv);
            $('<img/>').attr('src', thumb.attr('src')).css({height: '100%', width: 'auto', border: 0}).appendTo(thumbDiv);
        }
    }
    $(this).empty().append(transDiv).append(contentDiv).hover(function() {
        $(this).children('.background').stop().fadeTo(boxContentFadeSpeed, 1);
    }, function() {
        $(this).children('.background').stop().fadeTo(boxContentFadeSpeed, boxContentOpacity);
    });
});

// remove extra article thumbnail for article list
$('img').remove('.article-thumbnail');

// selected category highlighted
if (data.currentCategory != undefined) {
    $('a.category-link:contains("' + data.currentCategory + '")').addClass('selected');
}

// ------ PAGE ONLOAD EVENT ------
$(window).load(function() {
    // remove the loading indicator
    $('#loading-indicator').fadeOut('fast');
});

})(jQuery_wowbuty);

</script>
